//行别
Ufida.BankCategory = function(config){
	this.bankTpl = new Ext.XTemplate('</div><tpl for=".">'
		+ '	<div class="search-item">', '{text}', '	</div>' + '</tpl>');
		
	this.bank_store = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : '/scmMng/proCity.do?method=getBank'
		}),
		autoLoad:false,
		reader : new Ext.data.JsonReader({
			totalProperty : 'result.totalCount',
			root : 'result.result'
		}, [{
			name : 'name'
		}, {
			name : 'text'
		}])
	});
	
	Ufida.BankCategory.superclass.constructor.call(this, {
		store : this.bank_store,
		displayField : 'text',
		valueField : 'name',
		typeAhead : false,
		mode : 'remote',
		msgTarget : 'under',
		loadingText : '查找中...',
		triggerAction : 'all',
		selectOnFocus : true,
		hideTrigger : false,
		itemSelector : 'div.search-item',
		tpl :  this.bankTpl,
		listWidth : 250,
		queryDelay : 0,
		maxLength :9,
		listeners : {
			"blur" : function(v) {
				this.setValue(v.getRawValue())
			}
		}
	});
	
	Ext.apply(this, config);
}

Ext.extend(Ufida.BankCategory, Ext.form.ComboBox);

//业务员
Ufida.BusinessPeo = function(config){
	
	this.businesserTpl = new Ext.XTemplate('</div><tpl for=".">'
		+ '	<div class="search-item" >', '{text}', '	</div>' + '</tpl>');
		
	this.businesser_store = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : '/scmMng/proCity.do?method=getBusinesser'
		}),
		reader : new Ext.data.JsonReader({
			totalProperty : 'result.totalCount',
			root : 'result.result'
		}, [{
			name : 'name'
		}, {
			name : 'text'
		}])
	});
	
	Ufida.BusinessPeo.superclass.constructor.call(this, {
		readOnly : true,
		store : this.businesser_store,
		displayField : 'text',
		valueField : 'text',
		typeAhead : true,
		msgTarget : 'under',
		loadingText : '查找中...',
		mode : 'remote',
		triggerAction : 'all',
		selectOnFocus : true,
		itemSelector : 'div.search-item',
		tpl :  this.businesserTpl,
		listWidth : 180,
		anchor : '95%',
		listWidth : 250,
//		emptyText : '请选择',
		editable : false,
		queryDelay : 0
	});
	
	Ext.apply(this, config);
}

Ext.extend(Ufida.BusinessPeo, Ext.form.ComboBox);

//省
Ufida.ProvinceCombox = function(config){
	this.provinceTpl = new Ext.XTemplate('</div><tpl for=".">'
		+ '	<div class="search-item">', '{text}', '	</div>' + '</tpl>');
		
	this.province_store = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : '/scmMng/proCity.do?method=getProvinces'
		}),
		
		reader : new Ext.data.JsonReader({
			totalProperty : 'result.totalCount',
			root : 'result.result'
		}, [{
			name : 'name'
		}, {
			name : 'text'
		}])
	});
	
	Ufida.ProvinceCombox.superclass.constructor.call(this, {
		readOnly : true,
		store : this.province_store,
		displayField : 'text',
		valueField : 'name',
		typeAhead : true,
		msgTarget : 'under',
		loadingText : '查找中...',
		mode : 'remote',
		triggerAction : 'all',
		selectOnFocus : true,
		itemSelector : 'div.search-item',
		tpl :  this.provinceTpl,
		listWidth : 180,
		listWidth : 250,
//		emptyText : '请选择',
		editable : false,
		queryDelay : 0
	});
	
	Ext.apply(this, config);
}

Ext.extend(Ufida.ProvinceCombox, Ext.form.ComboBox);

//市
Ufida.CityCombox = function(config){
	
	this.cityTpl = new Ext.XTemplate('</div><tpl for=".">'
		+ '	<div class="search-item">', '{text}', '	</div>' + '</tpl>');
		
	this.city_store = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : '/scmMng/proCity.do?method=getCities'
		}),
		reader : new Ext.data.JsonReader({
			totalProperty : 'result.totalCount',
			root : 'result.result'
		}, [{
			name : 'name'
		}, {
			name : 'text'
		}])
	});
	
	Ufida.CityCombox.superclass.constructor.call(this, {
		readOnly : true,
		store : this.city_store,
		displayField : 'text',
		valueField : 'name',
		typeAhead : true,
		msgTarget : 'under',
		loadingText : '查找中...',
		mode : 'local',
		triggerAction : 'all',
		selectOnFocus : true,
		itemSelector : 'div.search-item',
		tpl :  this.cityTpl,
		listWidth : 180,
		listWidth : 250,
//		emptyText : '请选择',
		editable : false,
		queryDelay : 0
	});
	
	Ext.apply(this, config);
}

Ext.extend(Ufida.CityCombox, Ext.form.ComboBox);

//行名
Ufida.BankNameCombox = function(config){
	this.isBankNameSelect = false;
	this.cityTpl = new Ext.XTemplate('</div><tpl for=".">'
		+ '	<div class="search-item">', '{text}', '	</div>' + '</tpl>');
		
	this.city_store = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : '/scmMng/proCity.do?method=getBankName'
		}),
		reader : new Ext.data.JsonReader({
			totalProperty : 'result.totalCount',
			root : 'result.result'
		}, [{
			name : 'name'
		}, {
			name : 'text'
		}])
	});
	
	Ufida.BankNameCombox.superclass.constructor.call(this, {
		store : this.city_store,
		displayField : 'text',
		valueField : 'name',
		typeAhead : false,
		mode : 'remote',
		msgTarget : 'under',
		loadingText : '查找中...',
		triggerAction : 'all',
		selectOnFocus : true,
		hideTrigger:false,
		itemSelector : 'div.search-item',
		tpl :  this.cityTpl,
		listWidth : 180,
		listWidth : 250,
		pageSize:10,
		queryDelay : 0,
		listeners : {
			"blur" : function(v) {
				this.setValue(v.getRawValue())
			}
		}
	});
	
	Ext.apply(this, config);
}

Ext.extend(Ufida.BankNameCombox, Ext.form.ComboBox);

Ext.override(Ufida.BankNameCombox,{
	    doQuery : function(q, forceAll){
        if(q === undefined || q === null){
            q = '';
        }
        var qe = {
            query: q,
            forceAll: forceAll,
            combo: this,
            cancel:false
        };
        if(this.fireEvent('beforequery', qe)===false || qe.cancel){
            return false;
        }
        q = qe.query;
        forceAll = qe.forceAll;
        if(forceAll === true || (q.length >= this.minChars)){
                this.lastQuery = q;
                if(this.mode == 'local'){
                    this.selectedIndex = -1;
                    if(forceAll){
                        this.store.clearFilter();
                    }else{
                        this.store.filter(this.displayField, q);
                    }
                    this.onLoad();
                }else{
                    this.store.baseParams[this.queryParam] = q;
                    this.store.load({
                        params: this.getParams(q)
                    });
                    this.expand();
                }
        }
    }
})

//行业

Ufida.IndustryCombox = function(config){
	
	this.industryTpl = new Ext.XTemplate('</div><tpl for=".">'
		+ '	<div class="search-item">', '{text}', '	</div>' + '</tpl>');
		
	this.industry_store = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
		url : '/scmMng/proCity.do?method=getIndustry'
		}),
		reader : new Ext.data.JsonReader({
			totalProperty : 'result.totalCount',
			root : 'result.result'
		}, [{
			name : 'name'
		}, {
			name : 'text'
		}])
	});
	
	Ufida.IndustryCombox.superclass.constructor.call(this, {
		readOnly : true,
		store : this.industry_store,
		displayField : 'text',
		valueField : 'name',
		typeAhead : true,
		mode : 'remote',
		msgTarget : 'under',
		loadingText : '查找中...',
		triggerAction : 'all',
		selectOnFocus : true,
		itemSelector : 'div.search-item',
		tpl :  this.industryTpl,
		listWidth : 180,
		anchor : '95%',
		listWidth : 250,
//		emptyText : '请选择',
		editable : false,
		queryDelay : 0
	});
	
	Ext.apply(this, config);
}
	
Ext.extend(Ufida.IndustryCombox, Ext.form.ComboBox);

/*
 * provinceConfig:省自定义配置、cityConfig:市自定义配置、bankCategoryConfig:行别自定义配置、industryConfig:行业自定义配置、
 * businesserConfig:业务员自定义配置、
 */
Ufida.combox = function(provinceConfig, cityConfig, bankCategoryConfig,
		industryConfig, businesserConfig, bankNameConfig, flag){
	this.province = new Ufida.ProvinceCombox(provinceConfig);//省份下拉列表
	this.city = new Ufida.CityCombox(cityConfig);//市县下拉列表
	this.bankCategory = new Ufida.BankCategory(bankCategoryConfig);//行别下拉
	this.industry = new Ufida.IndustryCombox(industryConfig);//行业
	this.businesser = new Ufida.BusinessPeo(businesserConfig);
	this.bankName = new Ufida.BankNameCombox(bankNameConfig);
	flag = flag == undefined ? '' : flag;
	var self = this;
	
	//省下拉前初始化
	this.province.on('beforequery',function(){
		this.province.store.baseParams.bankFlag = this.province.bankProvince;//银行省
		this.province.store.baseParams.flag = this.province.flag;//普通省
	},this);
}


// 省份、市县联动
function bindProCity( province, city){
	var provinceId = new Ext.form.Hidden({});
	var provinceName = new Ext.form.Hidden({});
	var cityStore = city.store ;
	
	var cityItem = Ext.data.Record.create([{
		name : 'name'
	}, {name : 'text'
	}]);
	
	province.on('select',function(combox,record){
		provinceId.setValue(record.get('name'));
		provinceName.setValue(record.get('text'));
	});
	
	cityStore.on('load',function(store,records){
		if( !city.getValue() ){
			if(cityStore.getCount()==0){
				var r = new cityItem({
					'name'	: provinceId.getValue(),
					'text'	: provinceName.getValue()
				});
				cityStore.add(r);
			}
			if( cityStore.getCount() > 0 ){
				var record = cityStore.getAt(0);
				city.setValue(record.get("text"));
				city.fireEvent('select',city,record);
			}
		}
	});
	
	Ext.apply( provinceId,{
		setValue : function(v){	        
	        Ext.form.Hidden.superclass.setValue.call(this, v);
	        this.value = v;
	        
	        // 重新加载city中的数据
			cityStore.removeAll();
			var params = v || '';
			if( params != '' ){
				cityStore.baseParams.province = params;
				cityStore.baseParams.bankFlag = province.bankProvince;
				cityStore.baseParams.flag = province.flag;
				cityStore.reload();
			}
		}
	});
	
	// 更改'省份'后，'市县'中的内容置空
	province.on("beforeselect", function(combox, record) {
		if (combox.getValue() != record.data.name) {
			city.reset();
		}
	});
	
}